package com.zhengyisky.controller;

import com.zhengyisky.pojo.User;
import com.zhengyisky.service.UserService;
import com.zhengyisky.utils.Result;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${jwt.privateKey}")
    private String privateKey;

    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info("收到登陆请求,数据:{}", user);
        return userService.checkLogin(user);
    }

    @GetMapping("/getInfo")
    public User getInfo(String token){
        Claims claims = Jwts.parser().setSigningKey(privateKey).parseClaimsJws(token).getBody();
        Integer id = (Integer) claims.get("id");
        String username = (String) claims.get("username");
        Integer dept = (Integer) claims.get("dept");

        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setDeptId(dept);

        return user;
    }
}
